package com.egg.common.translate;

import com.egg.common.swagger.param.child.SwaggerLanguage;
import com.egg.common.translate.enums.I18nTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Optional;


@ComponentScan(basePackages = {"com.egg.common.swagger.param"})
public class CustomLocaleResolver implements LocaleResolver {


    @Autowired
    private SwaggerLanguage swaggerLanguage;


    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getHeader(swaggerLanguage.getName());
        Optional<Locale> opt = I18nTypeEnum.findLocaleByCode(lang);
        // 返回默认语言
        return opt.orElseGet(I18nTypeEnum::defaultLocale);
    }

    @Override
    public void setLocale(
            HttpServletRequest httpServletRequest
            , HttpServletResponse httpServletResponse
            , Locale locale
    ) {
    }


}